using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;

namespace ShopPowerTemplate.Util
{
    public class W8VisualTreeHelper
    {
        public static List<T> FindVisualChildren<T>(DependencyObject parent) where T : DependencyObject
        {
            return FindVisualChildrenRecursive<T>(parent, new List<T>());
        }

        private static List<T> FindVisualChildrenRecursive<T>(DependencyObject parent, List<T> children) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                T typeChild = child as T;
                if (typeChild != null)
                {
                    children.Add(child as T);
                }
                else
                {
                    FindVisualChildrenRecursive<T>(child, children);
                }
            }
            return children;
        }
    }
}
